package eworldpacs.widget;




import com.eworld.pacsviewer.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;

public class ToolBarButton extends LinearLayout implements View.OnTouchListener {

	public static final int DISABLED = 0;
	public static final int NORMAL = 1;
	public static final int PRESSED = 2;

	private static final String LOG_TAG = "ToolBarButton";

	private Drawable bg;

	private ImageView button = null;

	private FrameLayout imageView = null;

	private Drawable[] icon = new Drawable[3];

	private Context mContent;

	private boolean isInside;

	private boolean isPressed;

	private int state;

	private View.OnClickListener clickListener;

	public ToolBarButton(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		initToolBarButton(context);
	}

	public ToolBarButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		initToolBarButton(context);
	}

	protected void initToolBarButton(Context context) {
		setOnTouchListener(this);
		setEnabled(true);
		mContent = context;
		state = 1;
		addView(((LayoutInflater) getContext().getSystemService(
				Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.toolbarbutton, this, false));
		button = (ImageView) findViewById(R.id.toolbarBtn);
		imageView = (FrameLayout) findViewById(R.id.toolbarBg);
		imageView.setBackgroundDrawable(null);
		bg = mContent.getResources().getDrawable(R.drawable.tools_touch_bg);

	}

	public boolean isPressed() {
		return isPressed;
	};

	@Override
	public void setEnabled(boolean enabled) {
		// TODO Auto-generated method stub
		super.setEnabled(enabled);

		if (button == null || imageView == null) {
			return;
		}

		if (!isEnabled()) {
			button.setImageDrawable(icon[0]);
			imageView.setBackgroundDrawable(null);
			state = 0;
			return;
		}

		if (isPressed) {
			button.setImageDrawable(icon[2]);
			imageView.setBackgroundDrawable(bg);
			state = 2;
			return;
		}

		button.setImageDrawable(icon[1]);
		imageView.setBackgroundDrawable(null);
		state = 1;
	}

	public void setImage(int resid, int stateid) {

		if (stateid < 0 || stateid > 2 || mContent == null) {
			return;
		}

		icon[stateid] = mContent.getResources().getDrawable(resid);
		if (state == stateid)
			button.setImageDrawable(icon[stateid]);
	}

	public void setImage(Drawable drawable, int stateid) {
		if (stateid < 0 || stateid > 2) {
			return;
		}

		icon[stateid] = drawable;
		if (state == stateid)
			button.setImageDrawable(icon[stateid]);

	}

	public void setOnClickListener(OnClickListener l) {
		if (l instanceof View.OnClickListener) {
			clickListener = l;
		}
	}

	public void setPressedEx(boolean b) {
		isPressed = b;

		if (!isEnabled()) {
			return;
		}

		button.setImageDrawable(icon[1]);
		imageView.setBackgroundDrawable(null);

		if (isPressed()) {
			button.setImageDrawable(icon[2]);
			imageView.setBackgroundDrawable(bg);
		}

		requestLayout();
		invalidate();
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		if(!isEnabled())
			return true;
		
		float fX = event.getX();
		float fY = event.getY();
		if (fX < 0.0F || fX > getWidth() || fY < 0.0F || fY > getHeight()) {
			button.setImageDrawable(icon[1]);
			isInside = false;
			return true;
		}
		
		switch (event.getActionMasked()) {
		case MotionEvent.ACTION_DOWN:
			button.setImageDrawable(icon[2]);
			isInside = true;
			return true;
		case MotionEvent.ACTION_UP:
			button.setImageDrawable(icon[1]);
			break;
		case MotionEvent.ACTION_CANCEL:
			button.setImageDrawable(icon[1]);
			isInside = false;
			break;

		default:
			return true;
		}
		
		isInside = false;
		if (clickListener != null) {
			clickListener.onClick(this);
		}
		
		return true;
	}

}
